package top.yuzt.noticeserver.controller;


import cn.hutool.core.io.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import top.yuzt.noticeserver.entity.Groupmembers;
import top.yuzt.noticeserver.entity.Messagefiles;
import top.yuzt.noticeserver.entity.Messages;
import top.yuzt.noticeserver.mapper.MessagefilesMapper;
import top.yuzt.noticeserver.mapper.MessagesMapper;
import top.yuzt.noticeserver.service.impl.MessagefilesServiceImpl;
import top.yuzt.noticeserver.service.impl.MessagesServiceImpl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuzt
 * @since 2024-05-07
 */
@RestController
@RequestMapping("/messagefiles")
public class MessagefilesController {
    @Autowired
    private MessagefilesMapper messagefilesMapper;
    @Autowired
    private MessagefilesServiceImpl messagefilesService;
    @Autowired
    private MessagesServiceImpl messagesService;
    @Autowired
    private MessagesMapper messagesMapper;

    private final String ROOT_PATH = System.getProperty("user.dir") + File.separator + "Local_files";
    private List<File> fileList;
    private  Integer uuid;
    private List<Messagefiles> messagefilesList;
    private List<MultipartFile> multipartFiles ;

    @PostMapping("/init")
    public void setFileIO(){//初始化方法 建立文件缓存列表与此次行为id
        this.fileList = new ArrayList<>();
        this.messagefilesList = new ArrayList<>();
        this.uuid = generateUniqueGroupId();
        this.multipartFiles = new ArrayList<>();
        System.out.println("初始化成功");
    }

    @PostMapping("/upload")
    public Integer upload(MultipartFile file) throws IOException {
        multipartFiles.add(file);
        String orgFilename = file.getOriginalFilename();
        String mainFilename = FileUtil.mainName(orgFilename);
        String mainFiletype = FileUtil.extName(orgFilename);
        File parenFile = new File(ROOT_PATH);
        if(!parenFile.exists()){
            parenFile.mkdirs();
        }
        String realname = orgFilename;
        orgFilename = System.currentTimeMillis() + "_" +generateUniqueGroupId();//改名后的文件名
        String fin = orgFilename + "." +mainFiletype;//文件+文件后缀
        File saveFile = new File(ROOT_PATH + File.separator + fin);

        file.transferTo(saveFile);
        String url = "http://localhost:8080/" + "Local_files/" + fin;
        System.out.println("执行");
        boolean flag = true;
        System.out.println(messagesMapper.findByMessageID(uuid));
        while (flag){
            if(messagesMapper.findByMessageID(uuid).isEmpty()){
                //messagefiles.setMessageid(uuid);
                flag = false;
            }else {
                uuid = generateUniqueGroupId();
            }
        }
        String downloadlink = "http://localhost:8080/files/download/"+ fin;
        System.out.println("执行");
        Messagefiles messagefiles = new Messagefiles(uuid,url,realname,downloadlink,0);
        if(mainFiletype.equals("png") || mainFiletype.equals("jpg")){
            messagefiles = new Messagefiles(uuid,url,realname,downloadlink,1);
        }
        messagefilesList.add(messagefiles);//存入缓存列表
        return messagefiles.getMessageid();
    }

    @PostMapping("/save")
    public boolean savemessage(@RequestBody Messages messages) throws IOException {
        messages.setMessageid(uuid);
        System.out.println("这是Messageid:" + messages.getMessageid());

        for(Messagefiles x : messagefilesList){
                messagefilesService.save(x);
        }
        System.out.println("message:"+messages.toString());
        return messagesService.save(messages);
    }
    private Integer generateUniqueGroupId() {
        UUID uuid = UUID.randomUUID();
        return Math.abs(uuid.hashCode()); // 使用UUID的hashCode()方法生成一个哈希码，然后取绝对值确保为正数
    }

}
